package com.esms.controller;

import com.esms.dao.EmployeeMapper;
import com.esms.po.Attendance;
import com.esms.po.Employee;
import com.esms.po.Grand;
import com.esms.po.Hassessment;
import com.esms.service.ILogService;
import com.esms.service.impl.EmployeeAttendanceServerImpl;
import com.esms.utils.DateUtil;
import com.esms.utils.ScoreDeal;
import com.esms.vo.EmployeeAttendanceVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author 帅帅帅帅
 *
 */

@Controller
public class EmployeeAttendanceController {

	@Autowired
	private ILogService logService;
	
	@Autowired
	private EmployeeAttendanceServerImpl EmployeeAttendanceService;
	
	@Autowired
    private EmployeeMapper employeeMapper = null;

	@Autowired
	private ScoreDeal scoreDeal;
	
    @RequestMapping("employeeAttendance.do")
    @ResponseBody
    public EmployeeAttendanceVO findSelectiveSatisfaction(
    		@RequestParam(value="d_id", defaultValue="0") int fdid,
            @RequestParam(value="e_id", defaultValue="0") String fEid,
			@RequestParam(value="e_name", defaultValue="0") String eName,
			HttpSession httpSession) throws Exception {

		Integer eId = (Integer) httpSession.getAttribute("employeeId");
		// pageNum:起始页面 pageSize:每页的大小
		// PageHelper.startPage(pageNum,limit);
		// 查找条件，一定要紧跟在startPage后
	    List<Attendance> attendanceList = new ArrayList<Attendance>();
	    try{
			String quarter = DateUtil.dateTimeDeal();
		    Employee employee = employeeMapper.selectByPrimaryKey(eId);
		    String e_account = employee.geteAccount();
		    List<String> accountDBList = EmployeeAttendanceService.getDBaccount();
			for(int i=0;i<accountDBList.size();i++) {
				String accountDB = accountDBList.get(i);
				if (e_account.equals(accountDB)) {
					attendanceList = EmployeeAttendanceService.getEmployeeAttendanceList(e_account, fdid, fEid, eName,quarter);
					break;
				}
			}
	    }catch (Exception e){
	    	e.printStackTrace();
	    }

		// 设置前台需要的数据
	    EmployeeAttendanceVO employeeAttendanceVO = new EmployeeAttendanceVO();
	    employeeAttendanceVO.setCode(0);
	    employeeAttendanceVO.setMsg("");
	    employeeAttendanceVO.setCount(attendanceList.size());
	    employeeAttendanceVO.setData(attendanceList);
		return employeeAttendanceVO;
	}
    
    @RequestMapping("updateAttendance.do")
    @ResponseBody
    public EmployeeAttendanceVO updateBonus(
    		@RequestParam("list") String list,
            HttpSession httpSession) throws Exception {
    	int req = 0;
    	String reMsg;
    	ObjectMapper mapper = new ObjectMapper();
    	JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, Attendance.class);
    	List<Attendance> listAttendance =  (List<Attendance>)mapper.readValue(list, jt);
    	/*我的工号查询*/
    	Integer eId = (Integer) httpSession.getAttribute("employeeId");
    	Employee employee = employeeMapper.selectByPrimaryKey(eId);
    	reMsg = "出勤考核评价成功";
		logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
				"出勤考核评价成功");
    	//获取系统当前时间
    	String quarter = DateUtil.dateTimeDeal();
        String systemTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));


        ArrayList<Hassessment> hassessList = new ArrayList<Hassessment>();
    	for(int i=0;i<listAttendance.size();i++) {
    		Attendance grandAttendance = listAttendance.get(i);
			Hassessment hassessment = new Hassessment();
    		/*输入分数判断*/
    		String score = grandAttendance.gethScore();
		    String score1 = grandAttendance.gethScore1();
		    if (score== null) score="0";
		    if (score1== null) score1="0";
		    String hScoresum = ScoreDeal.scoreCalculationCqkh(score,score1);
    		/*设置同级评分表*/
		    System.out.println(22222222);
		    System.out.println(hScoresum);
    		hassessment.sethAuserid(grandAttendance.getDid());
    		hassessment.sethAccount(employee.geteAccount());
    		hassessment.sethDuserid(grandAttendance.geteAccount());
    		hassessment.sethCreateTime(systemTime);
    		hassessment.sethCreateQuarter(quarter);
    		hassessment.sethScore(score);
    		hassessment.sethScore1(score1);
		    hassessment.sethScoresum(hScoresum);
    		hassessment.sethRmk(grandAttendance.gethRmk());
            hassessList.add(hassessment);

    		req = EmployeeAttendanceService.deleteAttendance(employee.geteAccount(),grandAttendance.geteAccount(),quarter);
    		if(req < 0) {
       		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"出勤考核评价失败");
    		}
    		System.out.println(444444);
            /*** 改为统一提交
    		req = EmployeeAttendanceService.insertAttendance(hassessment);
    		if(req < 0) {
          		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"出勤考核评价失败");
       		}
             ****/
        }
        req = EmployeeAttendanceService.addhasessList(hassessList);
        if(req < 0) {
            reMsg = "评价失败";
            System.out.println(777777);
            logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
                    "出勤考核评价失败");
        }
        //设置前台需要的数据
    	EmployeeAttendanceVO employeeAttendanceVO = new EmployeeAttendanceVO();
    	employeeAttendanceVO.setCode(0);
    	employeeAttendanceVO.setMsg(reMsg);
    	employeeAttendanceVO.setData(listAttendance);
        return employeeAttendanceVO;
    }

}
